package com.ktgj.hotel.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import static java.util.Comparator.comparing;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.hotel.common.ElongUtils;
import com.ktgj.hotel.domain.base.HotelDetailInput;
import com.ktgj.hotel.domain.base.HotelDetailOutput;
import com.ktgj.hotel.domain.base.HotelDetailResponse;
import com.ktgj.hotel.domain.common.Hotels;
import com.ktgj.hotel.domain.rooms.HotelIncrDataRequest;
import com.ktgj.hotel.domain.rooms.HotelIncrDataResponse;
import com.ktgj.hotel.domain.rooms.HotelIncrIdRequest;
import com.ktgj.hotel.domain.rooms.HotelIncrIdResponse;
import com.ktgj.hotel.mapper.DicCityMapper;
import com.ktgj.hotel.mapper.HotelMapper;
import com.ktgj.hotel.utils.GaoDeUtil;
import com.ktgj.hotel.utils.HotelUtils;
import com.ktgj.hotel.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.hotel.mapper.HotelSupplierLinkMapper;
import com.ktgj.hotel.domain.HotelSupplierLink;
import com.ktgj.hotel.service.IHotelSupplierLinkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@Service
public class HotelSupplierLinkServiceImpl implements IHotelSupplierLinkService 
{
    @Autowired
    private HotelSupplierLinkMapper hotelSupplierLinkMapper;

    @Autowired
    private HotelServiceImpl hotelService;

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private DicCityMapper dicCityMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public HotelSupplierLink selectHotelSupplierLinkById(Long id)
    {
        return hotelSupplierLinkMapper.selectHotelSupplierLinkById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param hotelSupplierLink 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HotelSupplierLink> selectHotelSupplierLinkList(HotelSupplierLink hotelSupplierLink)
    {
        return hotelSupplierLinkMapper.selectHotelSupplierLinkList(hotelSupplierLink);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param hotelSupplierLink 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHotelSupplierLink(HotelSupplierLink hotelSupplierLink)
    {
        hotelSupplierLink.setCreateTime(DateUtils.getNowDate());
        return hotelSupplierLinkMapper.insertHotelSupplierLink(hotelSupplierLink);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param hotelSupplierLink 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHotelSupplierLink(HotelSupplierLink hotelSupplierLink)
    {
        return hotelSupplierLinkMapper.updateHotelSupplierLink(hotelSupplierLink);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteHotelSupplierLinkByIds(Long[] ids)
    {
        return hotelSupplierLinkMapper.deleteHotelSupplierLinkByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteHotelSupplierLinkById(Long id)
    {
        return hotelSupplierLinkMapper.deleteHotelSupplierLinkById(id);
    }

    /**
     * 国内酒店-增量编号
     * @param request
     * @return
     */
    @Override
    public HotelIncrIdResponse hotelIncrId(HotelIncrIdRequest request) {
        HotelIncrIdResponse response = new HotelIncrIdResponse();
        HotelIncrIdResponse resp = new HotelIncrIdResponse();
        try {
            resp = (HotelIncrIdResponse) ElongUtils.getResult(response, request, HotelUtils.HOTEL_INCR_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 国内酒店-酒店增量
     * @param request
     * @return
     */
    @Transactional
    @Override
    public HotelIncrDataResponse hotelIncrData(HotelIncrDataRequest request) {
        HotelIncrDataResponse response = new HotelIncrDataResponse();
        HotelIncrDataResponse resp = new HotelIncrDataResponse();
        HotelDetailInput input = new HotelDetailInput();
        List<String> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        long hotelId = System.currentTimeMillis() / 1000;
        input.Options = "1,2,3,4";
        List<Long> hotelList = new ArrayList<>();
        try {
            resp = (HotelIncrDataResponse) ElongUtils.getResult(response, request, HotelUtils.HOTEL_INCR_DATA);
            List<String> collect = resp.getResult().getHotels().stream().map(item -> item.getHotelID()).collect(Collectors.toList());
            collect.forEach(s -> {
                if (!list.contains(s)) {
                    list.add(s);
                }
            });
            for (int i = 0; i < list.size(); i++) {
                String hotelCode = list.get(i);
                List<String> byHotel = hotelSupplierLinkMapper.queryByHotel(hotelCode);

                input.ArrivalDate = localDate.toString();
                input.DepartureDate = localDate.plusDays(1).toString();
                input.HotelIds = hotelCode;
                HotelDetailOutput hotelDetailOutput = ElongUtils.queryByIdHotel(input);
                if (byHotel == null || byHotel.size() <= 0) {
                    for (HotelDetailResponse.HotelsBean hotel : hotelDetailOutput.Result.getHotels()) {
                        Hotels hotels = new Hotels();
                        HotelSupplierLink hotelSupplierLink = new HotelSupplierLink();
                        for (HotelDetailResponse.HotelsBean.DetailBean detailBean : hotel.getDetail()) {
                            for (HotelDetailResponse.HotelsBean.DetailBean.ReviewBean reviewBean : detailBean.getReview()) {
                                //hotels.setCommentScore();
                                hotels.setCommentCount(Long.valueOf(reviewBean.getCount()));
                            }
                            hotels.setId(hotelId);
                            System.out.println(hotelId);
                            hotelList.add(hotelId);
                            if (detailBean.getCityName() == null || detailBean.getCityName().equals("")){
                                GaoDeUtil gaoDeUtil = new GaoDeUtil();
                                detailBean.setCityName(gaoDeUtil.cityName(detailBean.getLongitude(), detailBean.getLatitude()));
                            }
                            List<Long> cityId = dicCityMapper.queryByCityName(detailBean.getCityName());
                            if (cityId != null && cityId.size() > 0) {
                                for (Long aLong : cityId) {
                                    hotels.setCityId(aLong);
                                    hotels.setCityName(detailBean.getCityName());
                                    //hotels.setCountyId();
                                    hotels.setCountyName(detailBean.getDistrictName());
                                    hotels.setHotelName(detailBean.getHotelName());
                                    hotels.setPinYin(TimeUtils.Converting(detailBean.getHotelName()));
                                    hotels.setJianPin(TimeUtils.getPinYinHeadChar(detailBean.getHotelName()));
                                    hotels.setPicPath(detailBean.getThumbNailUrl());
                                    //hotels.setTypeId();
                                    //hotels.setStarId();
                                    //hotels.setStarName();
                                    //hotels.setBrandId();
                                    //hotels.setBrandName();
                                    //hotels.setCircleId();
                                    //hotels.setCircleName();
                                    //hotels.setThemes();
                                    //hotels.setNearLandmark();
                                    //hotels.setFeatureName();
                                    //hotels.setActivelabelName();
                                    //hotels.setActivelabelNameBg();
                                    hotels.setAddress(detailBean.getAddress());
                                    hotels.setGoogleLat(new BigDecimal(detailBean.getLatitude()));
                                    hotels.setGoogleLon(new BigDecimal(detailBean.getLongitude()));
                                    //hotels.setBaiduLat();
                                    //hotels.setBaiduLon();
                                    hotels.setBaseMinPrice(new BigDecimal(hotel.getLowRate()));
                                    hotels.setIsValid(1);
                                    //hotels.setFacilitieIds();
                                    hotels.setRecommendLevel(5);
                                    hotels.setCtripRecommendLevel(2L);
                                    hotels.setJsjHotelId("0");
                                    hotels.setUpdateTime(new Date());
                                    hotels.setCreateTime(new Date());
                                    hotelMapper.insertHotel(hotels);
                                    hotelSupplierLink.setId(hotelId);
                                    hotelSupplierLink.setHotelId(hotels.getId());
                                    hotelSupplierLink.setCityId(hotels.getCityId());
                                    hotelSupplierLink.setHotelCode(hotelCode);
                                    hotelSupplierLink.setSupplierId(5L);
                                    hotelSupplierLink.setIsValid(1L);
                                    hotelSupplierLink.setCreateTime(hotels.getCreateTime());
                                    hotelSupplierLinkMapper.insertHotelSupplierLink(hotelSupplierLink);
                                }
                            }
                        }
                    }
                }
//                else {
//                    for (HotelDetailResponse.HotelsBean hotel : hotelDetailOutput.Result.getHotels()) {
//                        Hotels hotels = new Hotels();
//                        for (HotelDetailResponse.HotelsBean.DetailBean detailBean : hotel.getDetail()) {
//                            for (HotelDetailResponse.HotelsBean.DetailBean.ReviewBean reviewBean : detailBean.getReview()) {
//                                hotels.setCommentCount(Long.valueOf(reviewBean.getCount()));
//                            }
//                            hotels.setId(hotelId);
//                            Long cityId = dicCityMapper.queryByCityName(detailBean.getCityName());
//                            List<HotelSupplierLink> hotelSupplierLinks = hotelSupplierLinkMapper.selectHotelSupplierLinkByHotelCode(hotelCode);
//                            if (hotelSupplierLinks != null && hotelSupplierLinks.size() > 0){
//                                for (HotelSupplierLink hotelSupplierLink : hotelSupplierLinks) {
//                                    long time = hotelSupplierLink.getCreateTime().getTime();
//                                    hotelSupplierLink.setMillisecond(time);
//                                }
//                                hotelSupplierLinks.sort((y, x) -> Double.compare(y.getMillisecond(), x.getMillisecond()));
//                                hotels.setId(hotelSupplierLinks.get(0).getHotelId());
//                                hotels.setCityId(cityId);
//                                hotels.setCityName(detailBean.getCityName());
//                                hotels.setCountyName(detailBean.getDistrictName());
//                                hotels.setHotelName(detailBean.getHotelName());
//                                hotels.setPinYin(TimeUtils.Converting(detailBean.getHotelName()));
//                                hotels.setJianPin(TimeUtils.getPinYinHeadChar(detailBean.getHotelName()));
//                                hotels.setPicPath(detailBean.getThumbNailUrl());
//                                hotels.setAddress(detailBean.getAddress());
//                                hotels.setGoogleLat(new BigDecimal(detailBean.getLatitude()));
//                                hotels.setGoogleLon(new BigDecimal(detailBean.getLongitude()));
//                                hotels.setBaseMinPrice(new BigDecimal(hotel.getLowRate()));
//                                hotels.setIsValid(1);
//                                hotels.setRecommendLevel(5);
//                                hotels.setCtripRecommendLevel(2L);
//                                hotels.setJsjHotelId("0");
//                                hotels.setUpdateTime(new Date());
//                                hotels.setCreateTime(new Date());
//                                hotelMapper.updateHotel(hotels);
//                            }
//                        }
//                    }
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resp.hotels = hotelList;
        return resp;
    }
}
